ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
freemaster_sfio.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 *
00003 * Freescale Semiconductor Inc.
00004 * (c) Copyright 2004-2011 Freescale Semiconductor
00005 * ALL RIGHTS RESERVED.
00006 *
00007 ****************************************************************************/
00024 #include "freemaster.h"
00025 #include "freemaster_private.h"
00026 #include "freemaster_protocol.h"
00027 
00028 #if FMSTR_USE_SFIO && (!FMSTR_DISABLE)
00029 
00030 /* the sfiolib needs to be added to the project */
00031 #include "sfiolib.h"
00032 
00033 /***********************************
00034 *  local variables 
00035 ***********************************/
00036 
00037 static FMSTR_U8 pcm_nSfioRespLen;
00038 
00039 /* recorder runtime flags */
00040 static volatile union 
00041 {
00042     FMSTR_FLAGS all;
00043     
00044     struct 
00045     {
00046         unsigned bEvenRun : 1;     /* last command executed was the even one */
00047         unsigned bLastOK : 1;      /* last command executed properly */
00048     } flg;
00049     
00050 } pcm_wSfioFlags;
00051 
00052 /**************************************************************************/
00058 void FMSTR_InitSfio(void)
00059 {
00060     pcm_wSfioFlags.all = 0;
00061 }
00062 
00063 /**************************************************************************/
00075 FMSTR_BPTR FMSTR_SfioFrame(FMSTR_BPTR pMessageIO)
00076 {
00077     FMSTR_BPTR pResponse = pMessageIO;
00078     FMSTR_U8 i, nFrameLen, nByte;
00079     SFIO_U16 nRet = 0;
00080 
00081     /* get command and remember if it was even/odd */
00082     pMessageIO = FMSTR_ValueFromBuffer8(&nByte, pMessageIO);
00083     pcm_wSfioFlags.flg.bEvenRun = (nByte & 1) ? 0 : 1;
00084     pcm_wSfioFlags.flg.bLastOK = 0;
00085     
00086     /* get data length */
00087     pMessageIO = FMSTR_ValueFromBuffer8(&nFrameLen, pMessageIO);
00088 
00089     /* feed the SFIO engine byte-by-byte */
00090     for(i=0; nRet == 0 && i<nFrameLen; i++)
00091     {
00092         pMessageIO = FMSTR_ValueFromBuffer8(&nByte, pMessageIO);
00093         nRet = SFIO_ProcessRecievedChar(nByte);
00094     }
00095        
00096     /* frame not handled or handled prematurely */
00097     if(!nRet || i < nFrameLen)
00098         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_SFIOERR);
00099     
00100     /* how much data to return? */
00101     if(nRet > FMSTR_COMM_BUFFER_SIZE)
00102         return FMSTR_ConstToBuffer8(pResponse, FMSTR_STC_RSPBUFFOVF);
00103     
00104     /* remember this command had executed properly */
00105     pcm_nSfioRespLen = (FMSTR_U8) nRet;
00106     pcm_wSfioFlags.flg.bLastOK = 1;
00107     
00108     /* SFIO response to return */
00109     pResponse = FMSTR_ConstToBuffer8(pResponse, FMSTR_STS_OK | FMSTR_STSF_VARLEN);
00110     pResponse = FMSTR_ValueToBuffer8(pResponse, pcm_nSfioRespLen);
00111     return FMSTR_CopyToBuffer(pResponse, (FMSTR_ADDR) SFIO_GetOutputBuffer(), pcm_nSfioRespLen);
00112 }
00113 
00114 /**************************************************************************/
00135 FMSTR_BPTR FMSTR_SfioGetResp(FMSTR_BPTR pMessageIO)
00136 {
00137     FMSTR_U8 nByte;
00138     
00139     /* get command and determine if it is even/odd */
00140     FMSTR_ValueFromBuffer8(&nByte, pMessageIO);
00141 
00142     /* last command must have been finished propely */
00143     if(!pcm_wSfioFlags.flg.bLastOK)
00144          return FMSTR_ConstToBuffer8(pMessageIO, FMSTR_STC_SFIOERR);
00145 
00146     /* only respond to "matching" request (even to even, odd to odd) */
00147     if(nByte & 1)
00148     {
00149         if(pcm_wSfioFlags.flg.bEvenRun)
00150             return FMSTR_ConstToBuffer8(pMessageIO, FMSTR_STC_SFIOUNMATCH);
00151     }
00152     else
00153     {
00154         if(!pcm_wSfioFlags.flg.bEvenRun)
00155             return FMSTR_ConstToBuffer8(pMessageIO, FMSTR_STC_SFIOUNMATCH);
00156     }    
00157         
00158     /* SFIO response to return */
00159     pMessageIO = FMSTR_ConstToBuffer8(pMessageIO, FMSTR_STS_OK | FMSTR_STSF_VARLEN);
00160     pMessageIO = FMSTR_ValueToBuffer8(pMessageIO, pcm_nSfioRespLen);
00161     return FMSTR_CopyToBuffer(pMessageIO, (FMSTR_ADDR) SFIO_GetOutputBuffer(), pcm_nSfioRespLen);
00162 }
00163 
00164 #endif /* FMSTR_USE_SFIO && (!FMSTR_DISABLE) */